"""
python gitlab 工具
"""

import gitlab

class syncGitlab:

    def __init__(self, url, accessToken):
        self.url = url
        self.accessToken = accessToken

    def login(self):
        gl = gitlab.Gitlab(self.url, self.accessToken)
        return gl

    def project_list(self):
        gl = self.login()
        return gl.projects.list(all=True, sort="asc")

    def get_project(self, projectId):
        gl = self.login()
        return gl.projects.get(projectId)

    def get_project_by_name(self, projectName):
        gl = self.login()
        return gl.projects.get(projectName)

    # 下载文件内容
    def get_content(self, project, branch, path, to):
        with open(to, 'wb') as code:
            project.files.raw(file_path = path, ref = branch, streamed = True, action = code.write)
            print('download over:', path)


if __name__ == "__main__":
    sg = syncGitlab("https://gitlab.ecouser.net", "ncc-n-wry86YFsntwSjt")
    sg.get_content()
    # projects = sg.project_list()
    # for p in projects:
    #     print(p.id, p.name)

    # project = sg.get_project_by_name("arch/product_design_app")
    # print(project.id, project.name)


